import request from '@/utils/request';
import { downloadFileByData } from '@/utils/download';
import type { AxiosResponse } from 'axios';

// 可根据实际后端返回结构调整类型
export interface TableQuery {
  pageNo?: number;
  pageSize?: number;
  tableName_LIKE?: string;
  [key: string]: any;
}

export interface TableDetail {
  id?: string | number;
  [key: string]: any;
}

export function getList(data: TableQuery): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/findPage',
    method: 'get',
    params: data,
  });
}

export function doInsert(data: TableDetail): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/insert',
    method: 'post',
    data,
  });
}

export function doUpdate(data: TableDetail): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/update',
    method: 'post',
    data,
  });
}

export function doDelete(data: { id: string | number }): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/del',
    method: 'post',
    params: data,
  });
}

export function doDeleteAll(data: { ids: (string | number)[] }): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/delAll',
    method: 'post',
    params: data,
  });
}

export function getSubList(data: any): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/get',
    method: 'get',
    params: data,
  });
}

export function doSync(data: any): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/sync',
    method: 'post',
    params: data,
  });
}

export function getTables(): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/getTables',
    method: 'get',
  });
}

export function doImportTables(data: any): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/importTables',
    method: 'post',
    params: data,
  });
}

export function getGenLogs(data: any): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/logs/getByTableId',
    method: 'get',
    params: data,
  });
}

/**
 * 生成代码
 * @param data
 * @returns void
 */
export function doCreate(data: any): void {
  let requestURL = '/api/v1/generator/logs/create';
  // 下载文件
  downloadFileByData(requestURL, data);
}

export function doCreateMenu(data: any): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/logs/createMenu',
    method: 'post',
    params: data,
  });
}

export function doCreateJson(data: any) {
  return {
    url: '/api/v1/generator/logs/create',
    method: 'get',
    params: data,
  };
}

export function getFieldTypes(): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/getFieldTypes',
    method: 'get',
  });
}

export function getJavaFieldTypes(): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/getJavaFieldTypes',
    method: 'get',
  });
}

export function getJavaFieldTypesBySafety(): Promise<AxiosResponse<any>> {
  return request({
    url: '/api/v1/generator/table/getJavaFieldTypesBySafety',
    method: 'get',
  });
} 